import numpy as np
import matplotlib.pyplot as plt

data=np.loadtxt('logicData0.txt',delimiter=',')
x=data[:,:-1]
y=data[:,-1]

miu=np.mean(x)
sigma=np.std(x)
x=(x-miu)/sigma

X=np.c_[np.ones(len(x)),x]

def model(x,theta):
    return x.dot(theta)

def sigmoid(z):
    return 1/(1+np.exp(-z))

def cost(h,y):
    m=len(y)
    return -1/m*np.sum(y*np.log(h)+(1-y)*np.log(1-h))

def grad(x,y,iter0=1000,alpha=0.1):
    m,n=x.shape
    theta=np.zeros(n)
    J=np.zeros(iter0)
    for i in range(iter0):
        z=model(x,theta)
        h=sigmoid(z)
        J[i]=cost(h,y)
        dt=1/m*x.T.dot(h-y)
        theta-=alpha*dt
    return theta,h,J

def score(h,y):
    return np.mean(y==[h>0.5])

if __name__ == '__main__':
    theta,h,J=grad(X,y)
    plt.plot(J)
    plt.show()
    print(score(h,y))

    plt.scatter(x[:,0],x[:,1],c=y)
    min_x1=np.min(x[:,0])
    max_x1=np.max(x[:,0])

    min_x2=-(theta[0]+theta[1]*min_x1)/theta[2]
    max_x2=-(theta[0]+theta[1]*max_x1)/theta[2]

    plt.plot([min_x1,max_x1],[min_x2,max_x2],c='r')
    plt.show()